package com.example.seckill.error;

import com.example.seckill.filter.util.TraceIdContext;
import com.example.seckill.response.CommonReturnType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 全局异常处理器
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {




    /**
     * 某一列重复了
     */
    @ExceptionHandler(DuplicateKeyException.class)
    @ResponseStatus(HttpStatus.OK)
    public Object duplicateKeyException(HttpServletRequest request, DuplicateKeyException e) {
        log.error("数据库里面这一列是唯一索引,不允许重复 "+e);
        Map<String, Object> map = new HashMap<>();
        map.put("errCode", CommonErrorEnum.DUPLICATEKEY_ERROR.errCode);
        String message = e.getMessage();
        if (message.contains("user_info.unique_index_telphone")) {
            map.put("errMsg", "用户的手机号已使用");
        }else if(1==1){

        }else {
            map.put("errMsg", CommonErrorEnum.DUPLICATEKEY_ERROR.errMsg);
        }
        map.put("requestId", TraceIdContext.getTraceId());
        return CommonReturnType.create("fail",map);
    }
    /**
     * 业务异常
     */
    @ExceptionHandler(BusinessException.class)
    @ResponseStatus(HttpStatus.OK)
    public Object businessException(HttpServletRequest request, BusinessException e) {
        log.error("业务异常"+e);
//        Map<String, Object> map = new HashMap<>();
//        map.put("errCode", e.getErrCode());
//        map.put("errMsg", e.getErrMsg());
//        map.put("requestId", TraceIdContext.getTraceId());
        return CommonReturnType.create("fail",e.getCommonError());
    }

    /**
     * 运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseStatus(HttpStatus.OK)
    public Object runtimeException(HttpServletRequest request, RuntimeException e) {
        log.error("运行时异常",e);
        Map<String, Object> map = new HashMap<>();
        map.put("errCode", CommonErrorEnum.UNKNOWN_ERROR.getErrCode());
        map.put("errMsg",  CommonErrorEnum.UNKNOWN_ERROR.getErrMsg());
        map.put("requestId", TraceIdContext.getTraceId());
        return CommonReturnType.create("fail",map);
    }

    /**
     * 405异常
     */
    @ExceptionHandler(ServletRequestBindingException.class)
    @ResponseStatus(HttpStatus.OK)
    public Object servletRequestBindingException(HttpServletRequest request, ServletRequestBindingException e) {
        log.error("路由绑定异常",e);
        Map<String, Object> map = new HashMap<>();
        map.put("errCode", CommonErrorEnum.UNKNOWN_ERROR.getErrCode());
        map.put("errMsg",  "url绑定路由问题");
        map.put("requestId", TraceIdContext.getTraceId());
        return CommonReturnType.create("fail",map);
    }



    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.OK)
    public Object exception(HttpServletRequest request, Exception e) {
        log.error("非运行时异常",e);
        Map<String, Object> map = new HashMap<>();
        map.put("errCode", CommonErrorEnum.UNKNOWN_ERROR.getErrCode());
        map.put("errMsg",  CommonErrorEnum.UNKNOWN_ERROR.getErrMsg());
        map.put("requestId", TraceIdContext.getTraceId());
        return CommonReturnType.create("fail",map);
    }




}